package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description: wait和notify的练习
 */
public class ThreadDemo21 {
    private static Object locker = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                synchronized (locker) {
                    System.out.println("t1 wait前");
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("t1 wait后");
                    System.out.println("t1 notify前");
                    locker.notify();
                    System.out.println("t1 notify后");
                }
            }
        };
        t1.start();

        Thread.sleep(1000);

        Thread t2 = new Thread() {
            @Override
            public void run() {
                synchronized (locker) {
                    System.out.println("t2 notify前");
                    locker.notify();
                    System.out.println("t2 notify后");
                    System.out.println("t2 wait前");
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("t2 wait后");
                }
            }
        };
        t2.start();
    }
}
